import hitTheListServer from '../asyncRequest/hitTheList.ajax'
import util from '../util'

const SAVEQRCODE = 'SAVEQRCODE';
const MECELL = 'MECELL';
const GETSTATUS = 'GETSTATUS';
const GETCOUNT = 'GETCOUNT';
const ISUSE = 'ISUSE';
const GETTITLE = 'GETTITLE';
const GETMELADDER = 'GETMELADDER';
const LADDER = 'LADDER';
const SAVE_USER = 'SAVE_USER';


const initState = {
  user: {},
  url: '',
  userCount: 0,
  isUse: true,
  titOpt: {},
  meLadderData: {},
  ladderData: []
}

export function hitTheList(state = initState, payload) {
  switch (payload.type) {
    case SAVEQRCODE:
      return { ...state,
        url: payload.data
      }
    case GETCOUNT:
      return {
        ...state,
        userCount: payload.data
      }
    case MECELL:
      return {
        ...state,
        isUse: false,
        titOpt: {
          ...state.titOpt,
          current: payload.data
        }

      }
    case ISUSE:
      return {
        ...state,
        isUse: payload.data
      }
    case GETTITLE:
      return {
        ...state,
        titOpt: payload.data
      }
    case GETMELADDER:
      return {
        ...state,
        meLadderData: payload.data
      }
    case LADDER:
      return {
        ...state,
        ladderData: payload.data
      }
    case GETSTATUS:
      return {
        ...state,
        isUse: payload.data
      }
    case SAVE_USER:
      return {
        ...state,
        user: payload.data
      }
    default:
      return state
  }
}

export function getQrCode(params) {
  return async (dispatch) => {
    const data = await util.result(hitTheListServer.getQrCode(params))

    dispatch(saveQrCode(data))
  }
}

export function searchUrl(params) {
  return saveSearchUrl(params)
}

export function meCell(params) {
  return async (dispatch) => {
    const data = await util.result(hitTheListServer.meCell(params))
    if (!(typeof data === 'object')) {
      util.success('打榜成功')
      dispatch(saveMeCell(data))
    }
  }
}

export function status(params) {
  return async (dispatch) => {
    const data = await util.result(hitTheListServer.status(params))

    dispatch(getStatus(data))
  }
}

export function count(params) {
  return async (dispatch) => {
    const data = await util.result(hitTheListServer.count(params))

    dispatch(getCount(data))
  }
}

export function ladder(params) {
  return async (dispatch) => {
    const data = await util.result(hitTheListServer.ladder(params))

    dispatch(geLadder(data))
  }
}

export function meLadder(params) {
  return async (dispatch) => {
    const data = await util.result(hitTheListServer.meLadder(params))

    dispatch(getMeLadder(data))
  }
}

export function title(params) {
  return async (dispatch) => {
    const data = await util.result(hitTheListServer.title(params))

    dispatch(getTitle(data))
  }
}

function saveQrCode(data) {
  return {
    type: SAVEQRCODE,
    data: data
  }
}

function saveMeCell(data) {
  return {
    type: MECELL,
    data: data
  }
}

function getStatus(data) {
  return {
    type: GETSTATUS,
    data: data
  }
}

function getCount(data) {
  return {
    type: GETCOUNT,
    data: data
  }
}

function getTitle(data) {
  return {
    type: GETTITLE,
    data: data
  }
}

function getMeLadder(data) {
  return {
    type: GETMELADDER,
    data: data
  }
}

function geLadder(data) {
  return {
    type: LADDER,
    data: data
  }
}

function saveSearchUrl(data) {
  return {
    type: SAVE_USER,
    data: data
  }
}